package com.retallife.user.controller;

import com.retallife.user.exception.Result;
import com.retallife.user.pojo.Chat.ChatDeleteFriendVo;
import com.retallife.user.pojo.Chat.ChatFriendsVo;
import com.retallife.user.pojo.Chat.ChatMsgVo;
import com.retallife.user.pojo.Chat.ChatMsgToFromVo;
import com.retallife.user.service.Chat.ChatService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@CrossOrigin
@Api(tags = "聊天实体类")
@RequestMapping(method = {RequestMethod.POST, RequestMethod.GET})
public class ChatController {

    @Autowired
    ChatService chatService;

    @ApiOperation(value = "查询两人的聊天信息")
    @RequestMapping("LookTwoUserMsg")
    public List<ChatMsgVo> LookTwoUserMsg(@RequestBody ChatMsgToFromVo chatMsgToFromVo) {
        return chatService.LookTwoUserMsg(chatMsgToFromVo);
    }

    @ApiOperation(value = "查询某个用户的好友")
    @RequestMapping("LookUserAllFriends")
    public List<ChatFriendsVo> LookUserAllFriends(@RequestParam String userName) {
        return chatService.LookUserAllFriends(userName);
    }

    @ApiOperation(value = "用户添加好友")
    @RequestMapping("InsertUserFriend")
    public Result InsertUserFriend(@RequestBody ChatFriendsVo chatFriends) {
        //时间
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = simpleDateFormat.format(date);
        chatFriends.setAddtime(time);

        int result = chatService.InsertUserFriend(chatFriends);
        if (result > 0) {
            return Result.ok("添加用户成功");
        }
        return Result.ok("添加用户失败");
    }

    @ApiOperation(value = "用户删除好友")
    @RequestMapping("DeleteFriend")
    public Result DeleteFriend(@RequestBody ChatDeleteFriendVo chatDeleteFriendVo) {
        int result = chatService.DeleteChatFriend(chatDeleteFriendVo);
        int resulttwo = chatService.DeleteMsgFriend(chatDeleteFriendVo);
        if (result > 0 && resulttwo > 0) {
            return Result.ok("删除用户成功");
        }
        return Result.ok("删除用户失败");
    }

}
